﻿// Sequin.WebLogs.SearchWordHelper
//
// (c) 2011 Arthur Pitman
//
// Part of the Sequin Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License (Version 2.1) as published by the Free Software Foundation.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License (Version 2.1) along with this library; if not, write to the 
// Free Software Foundation, Inc., 
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
// or see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sequin.WebLogs
{
    /// <summary>
    /// A helper class for processing search words
    /// </summary>
    public class SearchWordHelper
    {
        /// <summary>
        /// Space splitter
        /// </summary>
        protected static char[] spaceSplitter = new char[] { ' ' };

        /// <summary>
        /// Extracts individual search words from a <see cref="string"/> by converting punctuation to white space and splitting it
        /// </summary>
        /// <param name="searchString">The search word <see cref="string"/> to split</param>
        /// <returns>An array of individual words</returns>
        public static string[] ExtractSearchWordsFromString(string searchString)
        {
            var ch = searchString.ToArray();
            for (int i = 0; i < ch.Length; i++)
            {
                if (!char.IsLetterOrDigit(ch[i]))
                    ch[i] = ' ';
            }
            return (new string(ch)).Split(spaceSplitter, StringSplitOptions.RemoveEmptyEntries);
        }
    }
}
